######################################
#模块概述：员工可以更新自己的个人档案
#作者：刘卓柳
#创建时间：2021.3.6
#更新时间：2021.3.15
######################################
# 导入数据库模块
import pymysql
# 导入Flask框架，这个框架可以快捷地实现了一个WSGI应用
from flask import Flask
from flask import flash
# 默认情况下，flask在程序文件夹中的templates子文件夹中寻找模块
from flask import render_template
# 导入前台请求的request模块
from flask import request, Blueprint, session
import traceback

# 传递根目录
updateWI=Blueprint('updateWI',__name__)

######################################
#函数概述：显示更新个人档案页面
######################################
@updateWI.route('/updateWorkerInfo', methods=['get', 'post'])
def updateWorkerInfo():
    if session.get('isadminan')!='员工':
        flash("您没有此操作权限！")
        return render_template('homepage.html')
    wid=session.get('uid')
    conn = pymysql.connect(host='139.217.107.173', user='root', password='123456', db='project_training',
                           charset='utf8')
    cur = conn.cursor()
    # SQL 查询语句
    sql = "select* from workerinfo WHERE ID ='%s'" % (wid)
    cur.execute(sql)
    content = cur.fetchall()
    conn.close()
    if len(content)==0:
        flash("请创建个人的员工档案！")
        return render_template('homepage2.html')
    else:
        worker = content[0]
        name = worker[1]
        sex = worker[2]
        birthday = worker[3]
        city = worker[4]
        tel = worker[5]
        email = worker[6]
        workstate = worker[7]
        cname = worker[8]
        major = worker[9]
        school = worker[10]
        education = worker[11]
        return render_template('updateWorkerInfo.html', name=name, sex=sex, birthday=birthday,
                               city=city,tel=tel,email=email, workstate=workstate,
                               cname=cname, major=major, school=school, education=education)


######################################
#函数概述：提交修改个人档案语句
######################################
@updateWI.route('/submitupdateWorkerInfo', methods=['get', 'post'])
def submitupdateWorkerInfo():
    name = request.values.get('name')
    sex = request.values.get('sex')
    birthday = request.values.get('birthday')
    city = request.values.get('city')
    tel = request.values.get('tel')
    email = request.values.get('email')
    workstate = request.values.get('workstate')
    cname = request.values.get('cname')
    major = request.values.get('major')
    school = request.values.get('school')
    education = request.values.get('education')

    sql="UPDATE workerinfo "
    cnt=0
    if name !='':
        cnt+=1
        sql+="SET Name='%s'"%(name)
    if sex !='':
        cnt+=1
        if cnt == 1:
            sql+="SET Sex='%s'"%(sex)
        else:
            sql+=",Sex='%s'"%(sex)
    if birthday !='':
        cnt+=1
        if cnt == 1:
            sql+="SET Birthday='%s'"%(birthday)
        else:
            sql+=",Birthday='%s'"%(birthday)
    if city !='':
        cnt+=1
        if cnt == 1:
            sql+="SET City='%s'"%(city)
        else:
            sql+=",City='%s'"%(city)
    if tel !='':
        cnt+=1
        if cnt == 1:
            sql+="SET tel='%s'"%(tel)
        else:
            sql+=",tel='%s'"%(tel)
    if email !='':
        cnt+=1
        if cnt == 1:
            sql+="SET Email='%s'"%(email)
        else:
            sql+=",Email='%s'"%(email)
    if workstate !='':
        cnt+=1
        if cnt == 1:
            sql+="SET WorkState='%s'"%(workstate)
        else:
            sql+=",WorkState='%s'"%(workstate)
    if cname !='':
        cnt+=1
        if cnt == 1:
            sql+="SET CName='%s'"%(cname)
        else:
            sql+=",CName='%s'"%(cname)
    if major !='':
        cnt+=1
        if cnt == 1:
            sql+="SET Major='%s'"%(major)
        else:
            sql+=",Major='%s'"%(major)
    if school !='':
        cnt+=1
        if cnt == 1:
            sql+="SET School='%s'"%(school)
        else:
            sql+=",School='%s'"%(school)
    if education !='':
        cnt+=1
        if cnt == 1:
            sql+="SET Education='%s'"%(education)
        else:
            sql+=",Education='%s'"%(education)
    if cnt!= 0:
        wid = session.get('uid')
        sql+=" WHERE ID ='%s'"%(wid)
        print(sql)
        conn = pymysql.connect(host='139.217.107.173', user='root', password='123456', db='project_training',
                               charset='utf8')
        cur = conn.cursor()
        cur.execute(sql)
        conn.commit();
        conn.close()
    return render_template('homepage2.html')

